﻿using System;
using Csla.Xaml;
using Accounting.Library;
using System.ComponentModel;
using Utility;
using Accounting.Library.Utility;

namespace csla4Skeleton.UI.SL.NghiepVu.MuaSanPham.MSP_MuaDichVu
{
    public class MSP_MuaDichVu_ViewModel : ViewModel<Accounting.Library.MSP_MuaDichVu>
    {
        #region Event Declare
        public delegate void ChildList_AddedNew_EventHandler(object sender, ChildListAddedNewArgs e);
        public delegate void DKNV_EventHandler(object sender, DieuKienNghiepVuEventArgs e);
        public delegate void HangHoaIdChanged_EventHandler(object sender, HangHoaIdChangedEventArgs e);
        public delegate void DienGiaiChanged_EventHandler(object sender, DienGiaiChangedEventArgs e);
        public delegate void TKCoIdChanged_EventHandler(object sender, TKCoIdChangedEventArgs e);
        public delegate void ThueSuatIdChanged_EventHandler(object sender, ThueSuatIdChangedEventArgs e);

        public delegate void TKNoIdStartWithSix_EventHandler(object sender, TKNoIdStartWithSixEventArgs e);
        public delegate void DoiTuongTHCPIDChanged_EventHandler(object sender, DoiTuongTHCPIDChangedEventArgs e);

        public event ChildList_AddedNew_EventHandler ChildList_AddedNew;
        public event EventHandler LoadCompleteted;
        public event DKNV_EventHandler stateChanged;
        public event EventHandler TCHangHoaChanged;
        public event EventHandler LoaiTienTeChanged;
        public event EventHandler TongTienThanhToanChanged;
        public event EventHandler TongTienHangChanged;
        public event EventHandler TienThueGTGTChanged;
        public event EventHandler EnableButtonLapPhieu;    
        public event ThueSuatIdChanged_EventHandler ThueSuatIdChanged;  
        public event HangHoaIdChanged_EventHandler hangHoaIdChanged;
        public event DienGiaiChanged_EventHandler DienGiaiChanged;
        public event TKCoIdChanged_EventHandler TKCoIdChanged;
        public event TKNoIdStartWithSix_EventHandler TKNoIdStartWithSix;
        public event DoiTuongTHCPIDChanged_EventHandler DoiTuongTHCPIDChanged;
        
        #endregion

        #region Refresh Model
        public MSP_MuaDichVu_ViewModel()
        {
            BeginRefresh("NewMSP_MuaDichVu");
        }

        public MSP_MuaDichVu_ViewModel(int Khoa)
        {
            BeginRefresh("GetMSP_MuaDichVu", Khoa);
        }

        public void NewMSP_MuaDichVuViewModel()
        {
            BeginRefresh("NewMSP_MuaDichVu");
        }

        public void GetMSP_MuaDichVuViewModel(int Khoa)
        {
            BeginRefresh("GetMSP_MuaDichVu", Khoa);
        }

       
        public void SaveAndNew()
        {
            BeginSave();
            NewMSP_MuaDichVuViewModel();
        }

        public void SaveMuaDichVu()
        {
            BeginSave();
           
        }

        protected override void OnRefreshed()
        {
            base.OnRefreshed();
            if (Model != null)
            {
                Model.MSP_MuaDichVu_ChiTiets.AddedNew += new EventHandler<Csla.Core.AddedNewEventArgs<MSP_MuaDichVu_ChiTietChild>>(MSP_MuaDichVu_ChiTiets_AddedNew);
                Model.MSP_MuaDichVu_Thues.AddedNew += new EventHandler<Csla.Core.AddedNewEventArgs<MSP_MuaDichVu_ThueChild>>(MSP_MuaDichVu_Thues_AddedNew);
                Model.MSP_MuaDichVu_ChiPhiMuaDichVus.AddedNew += new EventHandler<Csla.Core.AddedNewEventArgs<MSP_MuaDichVu_ChiPhiMuaDichVuChild>>(MSP_MuaDichVu_ChiPhiMuaDichVus_AddedNew);
                Model.NhomDKNVChanged += new EventHandler < DieuKienNghiepVuEventArgs>(OnStateChanged);
                Model.TinhChatHangHoaChanged += new EventHandler(OnTinhChatHangHoaChanged);
                Model.NhomDKNVChanged += new EventHandler<DieuKienNghiepVuEventArgs>(OnStateChanged);
                Model.TinhChatHangHoaChanged += new EventHandler(OnTinhChatHangHoaChanged);
                Model.HangHoaIdChanged += new EventHandler<HangHoaIdChangedEventArgs>(OnHangHoaIdChanged);
                Model.TKCoChanged += new EventHandler<TKCoIdChangedEventArgs>(OnTKCoChanged);
                Model.LoaiTienTeChanged += new EventHandler(OnLoaiTienTeChanged);
                Model.ThueSuatIdChanged += new EventHandler<ThueSuatIdChangedEventArgs>(OnThueSuatIdChanged);
                Model.DienGiaiChanged += new EventHandler<DienGiaiChangedEventArgs>(OnDienGiaiChanged);
                Model.BusyEvent += new EventHandler<IsBusyEventArgs>(DoChangingBusy);
                Model.EnableButtonLapPhieu += new EventHandler(OnEnableButtonLapPhieu);
                Model.TKNoIdStartWithSix += new EventHandler<TKNoIdStartWithSixEventArgs>(OnTKNoIdStartWithSix);
                Model.DoiTuongTHCPIDChanged += new EventHandler<DoiTuongTHCPIDChangedEventArgs>(OnDoiTuongTHCPIDChanged);
                Model.TongTienThanhToanChanged += new EventHandler(OnTongTienThanhToanChanged);
                Model.TongTienHangChanged += new EventHandler(OnTongTienHangChanged);
                Model.TienThueGTGTChanged += new EventHandler(OnTienThueGTGTChanged);
                if (LoadCompleteted != null)
                {
                    LoadCompleteted(this, new EventArgs());
                }
            }
        }


        protected override void OnSaved()
        {
            base.OnSaved();
            OnRefreshed();
            
            if (EnableButtonLapPhieu != null)
            {
                EnableButtonLapPhieu(this, new EventArgs());
            }
        }

        #endregion

        #region Add MSP_MuaDichVu Details Item

        void MSP_MuaDichVu_ChiTiets_AddedNew(object sender, Csla.Core.AddedNewEventArgs<MSP_MuaDichVu_ChiTietChild> e)
        {
            OnChildList_AddedNew(sender, new ChildListAddedNewArgs("MSP_MuaDichVu_ChiTiets"));
        }

        void MSP_MuaDichVu_Thues_AddedNew(object sender, Csla.Core.AddedNewEventArgs<MSP_MuaDichVu_ThueChild> e)
        {
            OnChildList_AddedNew(sender, new ChildListAddedNewArgs("MSP_MuaDichVu_Thues"));
        }

        void MSP_MuaDichVu_ChiPhiMuaDichVus_AddedNew(object sender, Csla.Core.AddedNewEventArgs<MSP_MuaDichVu_ChiPhiMuaDichVuChild> e)
        {
            OnChildList_AddedNew(sender, new ChildListAddedNewArgs("MSP_MuaDichVu_ChiPhiMuaDichVus"));
        }

        public void AddMSP_MuaDichVu_ChiTietItem()
        {
            Model.MSP_MuaDichVu_ChiTiets.AddNewItem(Model.Khoa);
        }

        public void AddMSP_MuaDichVu_ThueItem()
        {
            Model.MSP_MuaDichVu_Thues.AddNewItem(Model.Khoa);
        }

        public void AddMSP_MuaDichVu_ChiPhiMuaDichVuItem()
        {
            Model.MSP_MuaDichVu_ChiPhiMuaDichVus.AddNewItem(Model.Khoa);
        }


        public void OnDoiTuongTHCPIDChanged(object sender, DoiTuongTHCPIDChangedEventArgs e)
        {
            if (DoiTuongTHCPIDChanged != null)
            {
                DoiTuongTHCPIDChanged(sender, e);
            }

        }
        protected virtual void OnChildList_AddedNew(object sender, ChildListAddedNewArgs e)
        {
            if (ChildList_AddedNew != null)
            {
                //Invokes the delegates.
                ChildList_AddedNew(sender, e);
            }
        }

        public void OnStateChanged(object sender, DieuKienNghiepVuEventArgs e)
        {
            if (stateChanged != null)
            {
                stateChanged(sender, e);
            }

        }
        public void OnHangHoaIdChanged(object sender, HangHoaIdChangedEventArgs e)
        {
            if (hangHoaIdChanged != null)
            {
                hangHoaIdChanged(sender, e);
            }

        }
        public void OnTKNoIdStartWithSix(object sender, TKNoIdStartWithSixEventArgs e)
        {
            if (TKNoIdStartWithSix != null)
            {
                TKNoIdStartWithSix(sender, e);
            }

        }
        public void OnTinhChatHangHoaChanged(object sender, EventArgs e)
        {
            if (TCHangHoaChanged != null)
            {
                TCHangHoaChanged(sender, e);
            }

        }
        public void OnLoaiTienTeChanged(object sender, EventArgs e)
        {
            if (LoaiTienTeChanged != null)
            {
                LoaiTienTeChanged(sender, e);
            }
        }

        public void OnThueSuatIdChanged(object sender, ThueSuatIdChangedEventArgs e)
        {
            if (ThueSuatIdChanged != null)
            {
                ThueSuatIdChanged(sender, e);
            }

        }
        public void OnTongTienThanhToanChanged(object sender, EventArgs e)
        {

            if (TongTienThanhToanChanged != null)
            {
                TongTienThanhToanChanged(this, new EventArgs());
            }

        }
        public void OnTongTienHangChanged(object sender, EventArgs e)
        {

            if (TongTienHangChanged != null)
            {
                TongTienHangChanged(this, new EventArgs());
            }

        }
        public void OnTienThueGTGTChanged(object sender, EventArgs e)
        {

            if (TienThueGTGTChanged != null)
            {
                TienThueGTGTChanged(this, new EventArgs());
            }

        }

        public void OnTKCoChanged(object sender, TKCoIdChangedEventArgs e)
        {
            if (TKCoIdChanged != null)
            {
                TKCoIdChanged(sender, e);
            }

        }

        public void OnDienGiaiChanged(object sender, DienGiaiChangedEventArgs e)
        {
            if (DienGiaiChanged != null)
            {
                DienGiaiChanged(sender, e);
            }

        }
        public void DoChangingBusy(bool busy)
        {
            IsBusy = busy;
        }
        public void DoChangingBusy(object sender, IsBusyEventArgs e)
        {
            IsBusy = e.Busy;
        }

        public void OnEnableButtonLapPhieu(object sender, EventArgs e)
        {
            if (EnableButtonLapPhieu != null)
            {
                EnableButtonLapPhieu(sender, e);
            }
        }
        #endregion

        #region Delete MSP_MuaDichVu Details Item

        public void DeleteMSP_MuaDichVu_ChiTietItem(int KhoaChild)
        {
            Model.MSP_MuaDichVu_ChiTiets.DeleteItem(KhoaChild);
        }

        public void DeleteMSP_MuaDichVu_ThueItem(int KhoaChild)
        {
            Model.MSP_MuaDichVu_Thues.DeleteItem(KhoaChild);
        }

        public void DeleteMSP_MuaDichVu_ChiPhiMuaDichVuItem(int KhoaChild)
        {
            Model.MSP_MuaDichVu_ChiPhiMuaDichVus.DeleteItem(KhoaChild);
        }

        #endregion

        #region Nghiep Vu So
        internal void GhiSo()
        {
            this.Model.SaveMode = BusinessConfiguration.Configuration.GhiSoSaveMode;
            BeginSave();
        }

        internal void BoGhi()
        {
            int loaiChungTuId = BusinessConfiguration.Configuration.LoaiChungTuId_MSP_MuaDichVu;
            int chungTuGocId = Model.Khoa;

            this.IsBusy = true;
            Accounting.Library.GhiSo.BoGhiService.BoGhi(loaiChungTuId, chungTuGocId, (o, ex) =>
            {
                if (ex.Error != null)
                    this.Model.ErrorMessage = ex.Error.Message;
                else
                    this.Model.ErrorMessage = ((Accounting.Library.GhiSo.BoGhiService)ex.Object).ErrorMessage;
                BeginRefresh("GetMSP_MuaDichVu", Model.Khoa);
            });
            this.IsBusy = false;
        }
        #endregion
    }
}
